package com.example.mapinformationandroid.UI;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.mapinformationandroid.ConstantData.Define;
import com.example.mapinformationandroid.Model.User;
import com.example.mapinformationandroid.R;
import com.example.mapinformationandroid.UI.CustomizeView.CricleImageView;
import com.example.mapinformationandroid.Util.GlideImageEngine;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.util.ArrayList;
import java.util.List;

public class PersionHome extends AppCompatActivity implements View.OnClickListener {
    private CricleImageView head_picture;
    private User user;
    private TextView user_name, user_support;
    private static final int REQUEST_CODE_CHOOSE = 0x13;
    private LinearLayout lv_examine;
    private Button logout_button;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_persion_home);
        initView();
        initData();
        setViewData();
    }


    private void initView() {
        head_picture = findViewById(R.id.img_head_picture);
        user_name = findViewById(R.id.tv_user_name);
        user_support = findViewById(R.id.my_support);
        lv_examine = findViewById(R.id.ly_examine);
        logout_button = findViewById(R.id.btn_exist_login);
        lv_examine.setOnClickListener(this);
        logout_button.setOnClickListener(this);
        findViewById(R.id.ly_my_setting).setOnClickListener(this);
        findViewById(R.id.ly_my_comment).setOnClickListener(this);
        findViewById(R.id.ly_my_information).setOnClickListener(this);
        findViewById(R.id.button_back).setOnClickListener(this);
    }

    private void initData() {
        user = new User();
        sharedPreferences = getSharedPreferences("user", Activity.MODE_PRIVATE);
        if (sharedPreferences != null) {
            user.setId(sharedPreferences.getInt(Define.UserDefine.USER_ID, 0));
            user.setName(sharedPreferences.getString(Define.UserDefine.USER_NAME, ""));
            user.setHead_picture(sharedPreferences.getString(Define.UserDefine.USER_HEAD_PICTURE, ""));
            user.setSupport(sharedPreferences.getInt(Define.UserDefine.USER_SUPPORT, 0));
            user.setToken(sharedPreferences.getString(Define.UserDefine.USER_TOKEN, ""));
            user.setExamine(sharedPreferences.getInt(Define.UserDefine.USER_EXAMINE, 0));
        }
    }

    private void setViewData() {
        if (user.getExamine() == 1) {
            lv_examine.setVisibility(View.VISIBLE);
        } else {
            lv_examine.setVisibility(View.GONE);
        }
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.defult_user_head); //添加占位图
        options.error(R.drawable.defult_user_head)
                .centerCrop();
        Glide.with(this).load(user.getHead_picture()).into(head_picture);
        user_name.setText(user.getName());
        user_support.setText(user.getSupport() + "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_back:
                finish();
                break;
            case R.id.img_head_picture:
                initPermission();
                break;
            case R.id.ly_my_setting:
                startActivity(new Intent(PersionHome.this, SettingsActivity.class));
                break;
            case R.id.ly_my_information:
                break;
            case R.id.ly_my_comment:
                break;
            case R.id.btn_exist_login:
                sharedPreferences.edit().clear().apply();
                startActivity(new Intent(PersionHome.this, Login.class));
                finish();
                break;
        }
    }

    //选择照片
    private void ChooserImg() {
        Matisse.from(PersionHome.this)
                .choose(MimeType.ofAll(), false)
                .capture(true)
                .captureStrategy(new CaptureStrategy(true, "com.example.mapinformationandroid.fileprovider"))
                .countable(true)
                .maxSelectable(1) // 图片选择的最多数量
                .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                .thumbnailScale(0.85f) // 缩略图的比例
                .imageEngine(new GlideImageEngine()) // 使用的图片加载引擎
                .forResult(REQUEST_CODE_CHOOSE); // 设置作为标记的请求码
    }

    private void initPermission() {


        if (ContextCompat.checkSelfPermission(PersionHome.this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(PersionHome.this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(PersionHome.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(PersionHome.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {//未开启定位权限
            //开启定位权限,200是标识码
            ActivityCompat.requestPermissions(PersionHome.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 200);
        } else {
            ChooserImg();
        }

    }


    @SuppressLint("CheckResult")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        RequestOptions options = new RequestOptions();
        options.centerCrop();
        if (resultCode == RESULT_OK) {// 操作成功了
            switch (requestCode) {
                case REQUEST_CODE_CHOOSE:// 图库选择图片
                    List<String> img_path = Matisse.obtainPathResult(data);
                    List<String> imgliststr = new ArrayList<>();
                    for (String path : img_path) {
                        imgliststr.add(path);
                        Glide.with(this)
                                .load(path)
                                .apply(options)
                                .into(head_picture);
                        user.setBitmap_path(path);
                        user.setBitmap_local_path(path);
                    }
                    Log.d("AddInformation", "onActivityResult拿到了" + imgliststr.size() + "张图片:" + imgliststr);
                    break;
            }
        }
    }

}
